//
//  NetworkHelper.m
//  ZhiHui
//
//  Created by qskj-ios on 2018/5/10.
//  Copyright © 2018年 com.qiansheng. All rights reserved.
//

#import "NetworkHelper.h"
#import "SupermarketUtil.h"

static NSString *const AppDomain = @"AppDomain";

@implementation NetworkHelper

+ (AFHTTPSessionManager *)sharedManager {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.requestSerializer.timeoutInterval = 30.0f;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
        manager.responseSerializer.acceptableContentTypes = [[NSSet alloc] initWithObjects:@"application/xml", @"text/xml",@"text/html", @"application/json",@"text/plain",nil];
    return manager;
}

+ (void)apiPost:(NSString *)method parameters:(NSMutableDictionary *)parameters completion:(RequestBlock)completion{
    NSString *url = [NSString stringWithFormat:@"%@%@",Url_main,method];
    
    DELog(@"URL ---- %@",url);
    DELog(@"parameters ---- %@",parameters);
    AFHTTPSessionManager *manager = [NetworkHelper sharedManager];
    [manager POST:url parameters:parameters progress:NULL success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (completion) {
            DELog(@"responseObject ---- %@",responseObject);
            NSError *error = nil;
            completion(responseObject, error);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (completion) {
            DELog(@"error ---- %@",error);
            completion(nil, error);
            [SVProgressHUD showErrorWithStatus:@"请求失败了o(╥﹏╥)o"];
        }
    }];
}

+ (void)apiGet:(NSString *)method parameters:(NSMutableDictionary *)parameters completion:(RequestBlock)completion{
    NSString *url = [NSString stringWithFormat:@"%@%@",Url_main, method];
    
    DELog(@"URL ---- %@",url);
    AFHTTPSessionManager *manager = [NetworkHelper sharedManager];
    [manager GET:url parameters:parameters progress:NULL success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (completion) {
            DELog(@"responseObject ---- %@",responseObject);
            NSError *error = nil;
            completion(responseObject, error);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (completion) {
            DELog(@"error ---- %@",error);
            completion(nil, error);
            [SVProgressHUD showErrorWithStatus:@"请求失败了o(╥﹏╥)o"];
        }
    }];
}

+ (id)apiResponseParse:(id)responseObject error:(NSError **)error {
    NSDictionary *data = nil;
    NSString *errorCode = nil;
    NSString *errorMsg = nil;
    
    NSDictionary *dic;
    if ([responseObject isKindOfClass:[NSData class]]) {
        dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
    } else {
        dic = responseObject;
    }
    
    DELog(@"log日志 ----  %@",dic);

    errorCode = dic[@"isok"];
    errorMsg = dic[@"mes"];
    data = dic[@"response"];
    
    id valueData = dic;
    
    BOOL isValid = (errorCode == nil || [errorCode intValue] == 1);

    if (!isValid) {
        valueData = nil;
        if (nil != error) {
            [SVProgressHUD dismiss];
            
            /** 根据后台文档设置 */
            *error = [NSError errorWithDomain:AppDomain code:[errorCode integerValue] userInfo:@{NSLocalizedDescriptionKey:errorMsg}];
            
            if ([errorCode integerValue] == 1000) {
                [[UserInfoManager sharedManager] goLogin];//token过期重新登录
            } else {
                [SVProgressHUD showErrorWithStatus:StringWith([errorMsg isEqualToString:@""] ? @"服务器访问失败,请稍后再试":errorMsg)];
            }
        }
    }
    if ([valueData isKindOfClass:[NSNull class]]) {
        valueData = nil;
    }
    return valueData;
}

@end
